package main

import "fmt"

func main() {
	nums := []int{2, 3, 4, 1000, 999, 1, 2}
	ans := nextGreaterElements2(nums)
	fmt.Println("ans", ans)
	fmt.Println("1%7", 8%7)
}

func nextGreaterElements(nums []int) []int {
	n := len(nums)
	//ans := make([]int, n)
	//for i := range ans {
	//	ans[i] = -1
	//}
	//stack := []int{}
	ret := []int{}
	for i := 0; i < n; i++ {
		temp := -1
		for j := i; j < 2*n-1; j++ {
			if nums[i] < nums[j%n] {
				temp = nums[j%n]
				break
			}

		}
		ret = append(ret, temp)
	}

	return ret
}

func nextGreaterElements2(nums []int) []int {
	n := len(nums)
	ans := make([]int, n)
	for i := range ans {
		ans[i] = -1
	}
	stack := []int{}
	for i := 0; i < n*2-1; i++ {
		for len(stack) > 0 && nums[stack[len(stack)-1]] < nums[i%n] {
			ans[stack[len(stack)-1]] = nums[i%n]
			stack = stack[:len(stack)-1]
		}
		stack = append(stack, i%n)
	}
	return ans
}
